//
//  RegisterUser.m
//  HiQ
//
//  Created by arvintan on 12-8-1.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "RegisterUser.h"
#import "EncryptorWithKey.h"

NSString *REGISTER_KEY =@"register";
static RegisterUser *sharedRegisterUser;

@implementation RegisterUser
@synthesize _delegate;

-(id) initWithHost:(NSString *)host andport:(int)port
{
    _connect = [[Connection alloc] initWithHostAddress:host andPort:port];
    id pdc = [[PacketDispathCenter alloc]init];
    //这里为了去除warning，以后再改
    (void)pdc;
    sharedRegisterUser = self;
    return self;
}

-(void) sendMsg:(NSString *)message
{
    [_connect sendMsg:message cmd:CMD_FUN_REGISTER subcmd:CMD_REG_REQUEST];
}

-(void) sendRegisterMsg:(NSString*) name Password:(NSString*)pwd HeadImage:(NSString*)image Sex:(NSString*) sex;
{
    NSData *nameData = [name dataUsingEncoding:NSUTF8StringEncoding];
    NSData *pwdData = [pwd dataUsingEncoding:NSUTF8StringEncoding];
    NSData *imageData = [image dataUsingEncoding:NSUTF8StringEncoding];
    NSData *sexData = [sex dataUsingEncoding:NSUTF8StringEncoding];
    NSUInteger nameLen = [nameData length];
    NSUInteger passwordLen = [pwdData length];
    NSUInteger ImageLen = [imageData length];
    //NSUInteger sexLen = [[sex dataUsingEncoding:NSUTF8StringEncoding] length];
    
    NSMutableData *data = [[NSMutableData alloc] initWithBytes:&(nameLen) length:sizeof(NSUInteger)];
    [data appendBytes:&(passwordLen) length:sizeof(NSUInteger)];
    [data appendBytes:&(ImageLen) length:sizeof(NSUInteger)];
    [data appendData:nameData];
    [data appendData:pwdData];
    [data appendData:imageData];
    [data appendData:sexData];
    
    NSData *encryptData = [EncryptorWithKey encryptWithKey:data forKey:REGISTER_KEY];
    [_connect sendData:encryptData cmd:CMD_FUN_REGISTER subcmd:CMD_REG_REQUEST];
    registerTimeOut = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(onRegisterTimeOut:) userInfo:nil repeats:NO];
}

-(void)onRegisterTimeOut:(NSTimer*) timer
{
    [registerTimeOut invalidate];
    dispatch_async(dispatch_get_main_queue(), ^{
        [_delegate registerTimeOut];
    });
}


+(id) sharedLoginServer
{
    return sharedRegisterUser;
}

-(void) onReceiveRegisterPacket:(NSData *)data 
{
    [registerTimeOut invalidate];
    void *bytes = (void*)[data bytes];
    NSUInteger subFun =*((NSUInteger *)(bytes + sizeof(NSUInteger)));
    NSUInteger userID =*((NSUInteger *)(bytes + 2*sizeof(NSUInteger)));
    
    switch (subFun) {
        case CMD_REG_RET_YES:
        {
            dispatch_async(dispatch_get_main_queue(), ^{
                [_delegate registerComplete:YES UserID:userID];            
            });
            break;
        }
        case CMD_REG_RET_NO:
        {
            dispatch_async(dispatch_get_main_queue(), ^{
                [_delegate registerComplete:NO UserID:userID];
            });
            break;
        }
        default:
        {
            NSLog(@"unknow register return");
            break;
        }
    }
}

@end
